package com.team.controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;

/**
 * @author 樊浩
 * @version 1.0
 * @description: TODO
 * @date 2020/12/29 10:37
 */
@Controller
public class DownLoadController {
 /*@RequestMapping("/download")
 public void download(HttpServletResponse response)throws Exception{
  //读取服务器文件的二进制数据
 FileInputStream fis=new FileInputStream("E:\\20201229145256.jpg");
 byte[] bs=new byte[fis.available()];  //字节数组
  fis.read(bs);
  fis.close();  //关闭流

  //将文件的内容输出到响应流中
  //设置相应下载文件头
  response.setHeader("Content-Disposition","attachment;fileName=old.jpg");
  //设置下载文件类型
  response.setContentType("image/jpg");
 //将数据写入到响应流即可
  ServletOutputStream sos=response.getOutputStream();
  sos.write(bs);
 }*/

 @RequestMapping("/download")
 public void download(String path, HttpServletRequest request, HttpServletResponse response)throws Exception {
  //读取服务器文件的二进制数据
  String absolutePath=request.getSession().getServletContext().getRealPath(path);
  FileInputStream fis = new FileInputStream(absolutePath);
  byte[] bs = new byte[fis.available()];  //字节数组
  fis.read(bs);
  fis.close();  //关闭流

  //将文件的内容输出到响应流中
  //设置相应下载文件头
  String downloadFileName=System.currentTimeMillis()+".apk";
  response.setHeader("Content-Disposition", "attachment;fileName="+downloadFileName);
  //设置下载文件类型
  response.setContentType("application/vnd.android.package-archive");
  //将数据写入到响应流即可
  ServletOutputStream sos = response.getOutputStream();
  sos.write(bs);
 }
}
